<?php
//this program will run every minute for sending emails.
set_time_limit(60*60);
ini_set('memory_limit', '128M');

$start = microtime(true);

require_once('config.php');
require_once dirname(__FILE__). '/util/ses.php';
require_once dirname(__FILE__). '/util/connection.class.php';

$ses = new SimpleEmailService(AWS_ACCESS_KEY, AWS_SECRET_KEY);

$conn = Connection::getInstance();
$sql = 'select * from emails where sent=0 order by priority desc, created asc limit 20';
$emails = $conn->query($sql);
foreach ($emails as $email)
{
	$m = new SimpleEmailServiceMessage();
	$m->addTo($email['sendto']);
	$m->setFrom('"'.$email['sender'].'" <'.$email['sendfrom'].'>');
	$m->setSubject($email['subject']);
	$m->setMessageFromString(null, $email['content']);
	$begin = microtime(true);
	$ses->sendEmail($m);
	$end = microtime(true);
	echo ($end-$begin). "\n";
	$now = time();
	$sql = "update emails set sent=? where id=?";
	$conn->execute($sql, array($now, $email['id']));
	usleep(100);
}

$end = microtime(true);

echo ($end-$start). "\n";

?>